from two_test import fontcolor_change as font
from prettytable import PrettyTable


class Book(object):
    def __init__(self, name, author, borrowed=0, borrow=0):
        """
        :param name:书籍名称
        :param author: 作者
        :param borrowed: 已借出书籍数
        :param borrow: 未借出的书籍数
        """
        self.name = name
        self.author = author
        self.borrowed = borrowed
        self.borrow = borrow

    def __str__(self):
        return 'bookname:{}'.format(self.name)

class BookManage():

    books = []

    def information(self):
        """初始化书籍信息的函数, 添加书籍信息"""

        BookManage.books.append(Book("Python核心编程", 'Wesley Chun', 5, 10))
        BookManage.books.append(Book("Python for data analysis", 'Wes McKinney', 5, 10))
        BookManage.books.append(Book("流畅的Python", 'Luciano Ramalho', 0, 10))
        print(font.Ok + "初始化书籍信息成功" +font.END )
        print(BookManage.books)



    def find(self, name):
        """        根据用户传入的书籍名称判断书籍是否存在, 如果存在, 返回该书籍对象; 如果不存在, 返回None        """
        for book in self.books:
            if book.name == name:
                return book
            else:
                return None

    def entering(self):
        name = input("请输入要录入的书籍名称：").strip()
        book = self.find(name)
        if book:
            print(font.warning + "该书信息已存在，请不要重复录入" + font.END)
        else:
            author = input("请输入作者：")
            borrow = input("请输入录入数量：")
            borrowed = 0
            self.books.append(Book(name, author, borrowed, int(borrow)))
            print(font.Ok + "录入书籍信息成功" + font.END)

    def borrowBook(self):  # 删除字符串左右的空格
        name = input("借阅书籍名称：").strip()
        # 查找书籍, 如果存在, 返回书籍对象, 不存在, 返回None;
        book = self.find(name)
        if book:
            if book.borrow > 0:
        # 未借出书籍数量减1;
                book.borrow -= 1
        # 借出书籍数量加1
                book.borrowed += 1
                print(font.Ok + "书籍借阅成功" + font.END)
            else:
                print(font.warning + "书籍%s库存不足" %(book.name) + font.END)
        else:
            print(font.Error + "书籍%s不存在" %(name) + font.END)


    def returnBook(self):
        name = input("归还书籍名称：").strip()
        book = self.find(name)
        if book:
            book.borrow += 1
            book.borrowed -= 1
            print(font.Ok + "书籍归还成功" + font.END)
        else:
            print(font.Error + "本图书馆没有此书" + font.END)


    def show(self):

        print("书籍信息显示".center(50, '*'))
        names = ["编号", "书籍名称", "作者", "已借出数量", "库存数量"]
        table = PrettyTable()
        table.field_names = ["编号", "书籍名称", "作者", "已借出数量", "库存数量"]
        for index, book in enumerate(self.books):
            table.add_row([index + 1, book.name, book.author, book.borrowed, book.borrow])

        print(table)


def main():
    # 创建书籍管理的对象
    bm = BookManage()
    # 先初始化书籍信息
    bm.information()
    prompt = """                
    书籍借阅与管理系统                            
    1).录入书籍信息            
    2).借阅书籍信息            
    3).归还书籍            
    4).查询书籍信息            
    5).退出系统                            
    请输入你的选择："""
    while True:
        choice = input(prompt)
        if choice == '1':
            bm.entering()
        elif choice == '2':
            bm.borrowBook()
        elif choice == '3':
            bm.returnBook()
        elif choice == '4':
            bm.show()
        elif choice == '5':
            exit(0)
        else:
            print("请输入正确的选项")

main()














